using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UserSorce.Entity.Models;

namespace UserLogicCore.Logic
{
    public class MatterService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();
        /// <summary>
        ///添加待办事项,输入清单号，添加待办事件的ID，待办事件，结束时间，和备注
        /// 成功返回true
        /// </summary>
        /// <param name="lsitID"></param>
        /// <param name="content"></param>
        /// <param name="overTime"></param>
        /// <param name="reamark"></param>
        public void AddEvent(int userId,int typeId,int listId, string content, DateTime overTime, string reamark)
        {
                Matter userBcklogThing = new Matter()
                {
                    MatterContent= content,
                    TypeId=typeId,
                    InventoryId = listId,
                    UserId = userId,
                    CreatTime = DateTime.Now,
                    IsFinished = false,
                    IsPastdue = false,
                    OverTime = overTime,
                     Remark = reamark,
                };
                _ctx.Matter.Add(userBcklogThing);
                _ctx.SaveChanges();
        }
        /// <summary>
        ///删除事项，传入事项的ID,
        /// </summary>
        /// <param name="content"></param>
        public void DeleteEvent(int events)
        {
            var backlog = _ctx.Matter.SingleOrDefault(item => item.MatterId == events);
            _ctx.Matter.Remove(backlog);
            _ctx.SaveChanges();

        }
        /// <summary>
        /// 查看所有事项
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matter> GetAllEvents(int inventoryId, int userId)
        {
            return _ctx.Matter.Where(prop => prop.UserId == userId && prop.InventoryId == inventoryId).ToList();
        }
        /// <summary>
        /// 获取未完成事项
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matter> GetUnfinishedEvent(int inventoryId, int userId)
        {
            return _ctx.Matter.Where(prop => prop.UserId == userId && prop.IsFinished == false && prop.InventoryId == inventoryId).ToList();
        }

        /// <summary>
        ///  //修改过期时间
        /// </summary>
        /// <param name="backlogId"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public bool AddOverTime(int backlogId, DateTime time)
        {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.OverTime = time;
                _ctx.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        ///  //添加备注
        /// </summary>
        /// <param name="backlogId"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public bool AddRemark(int backlogId, string remark)
        {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.Remark = remark;
                _ctx.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 查找事件
        /// </summary>
        /// <param name="backlogId"></param>
        /// <returns></returns>
        public Matter FindEvent(int backlogId)
        {
            return _ctx.Matter.SingleOrDefault(prop => prop.MatterId == backlogId);
        }
        /// <summary>
        /// ///完成某一待办事项,成功返回true
        /// </summary>
        /// <param name="state"></param>
        /// <param name="backlogId"></param>
        /// <returns></returns>
        public void ChangeEventSate(bool state, int backlogId)
        {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.IsFinished = state;
                _ctx.SaveChanges();
            }
        }
    }
}
